export function filterOutAllNullObjects(arr: any) {
  return arr.filter((obj: any) => {
    // 检查对象是否至少有一个非null的属性
    return Object.values(obj).some((value) => value !== null)
  })
}

export function filterNullResults(result: any) {
  result.results.forEach((item: any) => {
    item.data = filterOutAllNullObjects(item.data)
  })

  return result
}

//移除P标签
export function processHtml(html: string) {
  // 使用正则表达式匹配被其他标签包裹的p标签
  const wrappedPTagPattern = /<(\w+)[^>]*>(?:\s*)<p>([^<]*(?:<(?!\/p>)[^<]*)*)<\/p>(?:\s*)<\/\1>/g

  // 先处理被包裹的p标签
  html = html.replace(wrappedPTagPattern, (match, outerTag, content) => {
    // 保留外层标签和内容,只移除p标签
    return `<${outerTag}>${content}</${outerTag}>`
  })

  // 处理剩余的独立p标签
  html = html.replace(/<p>([^<]*(?:<(?!\/p>)[^<]*)*)<\/p>/g, '$1')

  return html
}

export function getJson(content: any) {
  if (typeof content === 'string') {
    const ll = content.split('</think>')
    const str = ll[ll.length - 1]
    const j = str.replace(/```json|```/g, '').trim()
    return JSON.parse(j)
  } else {
    return content
  }
}
